#include "groupchatlist.h"
// ========== GroupChatItem 实现 ==========
GroupChatItem::GroupChatItem(const QString& avatarPath, const QString& groupName,
                             const QString& lastMsg, const QString& time,
                             bool isMuted, QWidget* parent)
    : QWidget(parent) {
    // 主布局：头像 + 右侧（群名+消息 + 时间+免打扰）
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(10, 8, 10, 8);
    mainLayout->setSpacing(10);

    // 群头像
    m_avatarLabel = new QLabel(this);
    QPixmap avatar(avatarPath);
    if (avatar.isNull()) { // 头像加载失败时用默认图
        avatar = QPixmap(40, 40);
        avatar.fill(Qt::gray);
    }
    m_avatarLabel->setPixmap(avatar.scaled(40, 40, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    m_avatarLabel->setFixedSize(40, 40);
    mainLayout->addWidget(m_avatarLabel);

    // 左侧：群名 + 最新消息（垂直布局）
    QVBoxLayout* leftLayout = new QVBoxLayout();
    leftLayout->setContentsMargins(0, 0, 0, 0);
    leftLayout->setSpacing(3);

    m_groupNameLabel = new QLabel(groupName, this);
    m_groupNameLabel->setStyleSheet("font-weight: bold; color: #333;");
    leftLayout->addWidget(m_groupNameLabel);

    m_lastMsgLabel = new QLabel(lastMsg, this);
    m_lastMsgLabel->setStyleSheet("color: #666; font-size: 12px;");
    leftLayout->addWidget(m_lastMsgLabel);

    mainLayout->addLayout(leftLayout);

    // 右侧：时间 + 免打扰（垂直布局）
    QVBoxLayout* rightLayout = new QVBoxLayout();
    rightLayout->setContentsMargins(0, 0, 0, 0);
    rightLayout->setSpacing(3);
    rightLayout->setAlignment(Qt::AlignTop | Qt::AlignRight);

    m_timeLabel = new QLabel(time, this);
    m_timeLabel->setStyleSheet("color: #999; font-size: 12px;");
    rightLayout->addWidget(m_timeLabel);

    if (isMuted) {
        m_muteLabel = new QLabel(this);
        m_muteLabel->setPixmap(QIcon(":/images/mute.png").pixmap(16, 16)); // 免打扰图标
        m_muteLabel->setFixedSize(16, 16);
        rightLayout->addWidget(m_muteLabel);
    }

    mainLayout->addLayout(rightLayout);
    mainLayout->addStretch(); // 右侧拉伸，使内容对齐
}

void GroupChatItem::paintEvent(QPaintEvent* event) {
    QWidget::paintEvent(event);
    // 绘制 hover 效果（鼠标悬浮时背景变色）
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

// ========== GroupChatList 实现 ==========
GroupChatList::GroupChatList(QWidget* parent) : QWidget(parent) {
    initUI();
    initStyle();
}

void GroupChatList::initUI() {
    m_listWidget = new QListWidget(this);
    m_listWidget->setStyleSheet("QListWidget { border: none; }");
    m_listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    // 添加群聊项示例
    GroupChatItem* item1 = new GroupChatItem(
        ":/images/group1.png", "我的acm群",
        "徐宁博: OK", "星期二");
    GroupChatItem* item2 = new GroupChatItem(
        ":/images/group2.png", "物联网工程231通知群",
        "琚丽晴: @全体成员 贷款名单需要重新填写到这个表格...", "08/23");
    GroupChatItem* item3 = new GroupChatItem(
        ":/images/group3.png", "河南电子科技大学迎新群",
        "李志恒爸爸: 交给窦奕军", "06/14", true); // 免打扰

    // 将自定义控件加入 QListWidget
    QListWidgetItem* listItem1 = new QListWidgetItem(m_listWidget);
    listItem1->setSizeHint(item1->sizeHint());
    m_listWidget->setItemWidget(listItem1, item1);

    QListWidgetItem* listItem2 = new QListWidgetItem(m_listWidget);
    listItem2->setSizeHint(item2->sizeHint());
    m_listWidget->setItemWidget(listItem2, item2);

    QListWidgetItem* listItem3 = new QListWidgetItem(m_listWidget);
    listItem3->setSizeHint(item3->sizeHint());
    m_listWidget->setItemWidget(listItem3, item3);

    // 主布局（直接用 listWidget 作为主部件）
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(m_listWidget);
}

void GroupChatList::initStyle() {
    // 全局样式表
    setStyleSheet(R"(
        QWidget {
            background-color: #f5f5f5;
        }
        QListWidget {
            background-color: #fff;
        }
        GroupChatItem:hover {
            background-color: #f0f0f0;
        }
        QLabel {
            font-family: "Microsoft YaHei";
        }
    )");
}
